package edu.android.com;

import java.util.Stack;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

public class dirTopSort extends framework{

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Visibility ...
        EditText mStartNode = (EditText)findViewById(R.id.startNode);
        mStartNode.setVisibility(EditText.INVISIBLE);
        
        
        //Start process
        Button mPickStartNode = (Button)findViewById(R.id.pickStartNode);
        
        mPickStartNode.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startThis();
			}
		});
        
        Toast.makeText(getApplicationContext(), "Don't create any Cycles!!!", Toast.LENGTH_SHORT).show();
        
        setEdgePrefereces();
    }
	
	private void setEdgePrefereces(){
		edge.mOrientated = true;
		edge.mWithCost = false;
		edge.mWithFlow = false;
	}
	
	private void setDefault(){
		int len = mNodes.size();
		used = new boolean[len + 1];
		order = new int[len + 1];
		
		for(int i = 0;i <= len;i ++){
			order[i] = 0;
			used[i] = false;
		}
		
		for(int i = 0;i < len;i ++){
			mNodes.get(i).initialize();
		}
	}
	
	private void df(node nd){
		used[nd.getNodeNumber()] = true;
		
		for(int i = 0;i < nd.mEdges.size();i ++){
			node right = nd.mEdges.get(i).secondNode;
			
			if(used[right.getNodeNumber()] == false)
				df(right);
		}
		order[order[0]--] = nd.getNodeNumber();
	}
	private void startThis(){
		setDefault();
		order[0] = mmNrNodes;
		for(int i = 0;i < mNodes.size();i ++)
			if(used[mNodes.get(i).getNodeNumber()] == false)
				df(mNodes.get(i));
		
		showToScreen();
	}
	
	private void showToScreen(){
		showOrder();
	}
	private void showOrder(){
		String toast = "Topological Sort order:\n";
		order[0] = mmNrNodes;
		for(int i = 1;i <= order[0];i ++)
			toast = toast + order[i] + " ";
		
		mDisplayContent.setText(toast);
	}
}
